﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPMVCUtil.Helpers.Html.Grid;
using ASPMVCUtil.Util;
using ASPMVCUtil.Models;
using ASPMVCUtil.ExcelExporter;
using Domain;
using ShowcaseMVC5.Models;

namespace ASPMVCUtilShowcase.Controllers
{
    public class GridController : Controller
    {
        //
        // GET: /Grid/
        // GET: /Grid/Index

        public ActionResult Index()
        {
            GridParams productsParams = GridParams.create("Products",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            return View(products(productsParams));
        }

        //
        // GET: /Grid/CellFormatting

        public ActionResult CellFormatting()
        {
            GridParams productsParams = GridParams.create("Products",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            return View(products(productsParams));
        }

        //
        // GET: /Grid/Paged

        public ActionResult Paged()
        {
            GridParams productsParams = GridParams.create("Products",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            return View(products(productsParams, 10));
        }

        //
        // GET: /Grid/Dependent

        public ActionResult Dependent()
        {
            GridParams productsParams = GridParams.create("Products",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString), "Categories");

            GridParams categoriesParams = GridParams.create("Categories",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            return View(new Model2Objects<GridPagedList<Product>, GridPagedList<Category>>(
                products(productsParams, 10, categoriesParams.SelectedRowID),
                categories(categoriesParams)));
        }

        //
        // GET: /Grid/Dependent

        public ActionResult RowActions()
        {
            GridParams productsParams = GridParams.create("Products",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString), "Categories");

            GridParams categoriesParams = GridParams.create("Categories",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            return View(new Model2Objects<GridPagedList<Product>, GridPagedList<Category>>(
                products(productsParams, 10, categoriesParams.SelectedRowID),
                categories(categoriesParams)));
        }

        //
        // GET: /Grid/Complete

        public ActionResult Complete()
        {
            GridParams productsParams = GridParams.create("Products",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString), "Categories1");

            GridParams categories1Params = GridParams.create("Categories1",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            GridParams categories2Params = GridParams.create("Categories2",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            return View(new Model3Objects<GridPagedList<Product>, GridPagedList<Category>, GridPagedList<Category>>(
                products(productsParams, 10, categories1Params.SelectedRowID),
                categories(categories1Params),
                categories(categories2Params)));
        }

        //
        // DELETE: /Grid/_DeleteProducts

        [HttpDelete]
        public ActionResult _DeleteProducts(string id)
        {
            var prodcut = ProductsRepository.getProduct(int.Parse(id));
            return Json(new JsonResponse<GridAjaxActionResult>(JsonResponseEnum.SUCCESS,
                new GridAjaxActionResult(id, string.Format(ShowcaseMVC5.GridResources.Prodcuts_Delete_Success, prodcut.ToString()))));
        }

        //
        // DELETE: /Grid/_DeleteCategories

        [HttpDelete]
        public ActionResult _DeleteCategories(string id)
        {
            var category = CategoriesRepository.getCategory(int.Parse(id));
            if (ProductsRepository.getProducts(id).Count() > 0)
            {
                return Json(new JsonResponse<string>(JsonResponseEnum.FAILURE,
                    string.Format(ShowcaseMVC5.GridResources.Categories_Delete_Error, category.ToString())));
            }
            else
            {
                return Json(new JsonResponse<GridAjaxActionResult>(JsonResponseEnum.SUCCESS,
                    new GridAjaxActionResult(id, string.Format(ShowcaseMVC5.GridResources.Categories_Delete_Success, category.ToString()))));
            }
        }

        //
        // GET: /Grid/ProductsExcel

        public ActionResult ProductsExcel()
        {
            GridParams parametrosCategorias = GridParams.create("Categories",
                NameValueCollectionRouteValueDictionary.create(Request.QueryString));

            IEnumerable<Product> productos = ProductsRepository.getProducts(parametrosCategorias.SelectedRowID);

            IEnumerable<ExcelColumn<Product>> columns = new ExcelColumn<Product>[] {
                ExcelColumn<Product>.create("Id"),
                ExcelColumn<Product>.create("Name"),
                ExcelColumn<Product>.create("Price", "Price", new PriceTemplate<Product>()),
                ExcelColumn<Product>.create("Release", "Release", new DateTemplate<Product>()),
                ExcelColumn<Product>.create("Quantity"),
                ExcelColumn<Product>.create("Category", "Category", "Name")
            };

            return Excel(productos, "Products", columns);
        }

        private GridPagedList<Product> products(GridParams productsParams, int paging = 0, String category = null)
        {
            IEnumerable<Product> products = ProductsRepository.getProducts(category);

            if (productsParams.OrderBySpace != null)
            {
                products = products.AsQueryable().OrderBy(productsParams.OrderBySpace);
            }

            return new GridPagedList<Product>(products, productsParams, new string[] { "Id" }, paging);
        }

        private GridPagedList<Category> categories(GridParams categoriesParams, int paging = 0)
        {
            IEnumerable<Category> categories = CategoriesRepository.getCategories();

            if (categoriesParams.OrderBySpace != null)
            {
                categories = categories.AsQueryable().OrderBy(categoriesParams.OrderBySpace);
            }

            return new GridPagedList<Category>(categories, categoriesParams, new string[] { "Id" }, paging);
        }
    }
}
